
$(document).ready(init);

var lastEditRowIndex;

function init(){
	initData();
	renderPage();
	bindEvent();
}

function initData(){
	queryMode = _QUERY_MODE_SIMPLE;
}

function bindEvent(){
	$(document).bind(_EVENT_PROMPT_INFO,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_WARNING,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_ERROR,ComponentUtil.promptMsg);
	
	$('#showAdvancedQueryFormLink').click(ComponentUtil.showAdvancedQueryForm);
	$('#hideAdvancedQueryFormLink').click(ComponentUtil.hideAdvancedQueryForm);
	
	$('#simpleQueryBt').click(sendQueryResult);
	$('#advanceQueryBt').click(sendQueryResult);
	
}

function renderPage(){
	
	var addBt = {text:'新增权限',iconCls:'icon-add',handler:popupAddWindow};
	var removeBt = {text:'删除权限',iconCls:'icon-cancel',handler:confirmRemove };
	var acceptBt = {text:'保存变更',iconCls:'icon-ok',handler:acceptChanges };
	var rejectBt = {text:'取消变更',iconCls:'icon-undo',handler:rejectChanges };
	
	
	$("#privilegeTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:true,
		idField:'id',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/privilegeManager/listPrivilege',
		toolbar:[addBt,'-',removeBt,'-',acceptBt,'-',rejectBt,'-'],
		loadMsg:'加载数据中,请稍等...',
		onDblClickRow:editPrivilege,
		onAfterEdit:commitPrivilegeUpdate
	});
	
	ComponentUtil.localizePagination($('#privilegeTable').datagrid('getPager'));

	
	$("#privilegeDetailDiv").dialog({
		width:450,
		height:250,
		resizable:false,
		closed:true
	});
	$.messager.defaults = {ok:'确认',cancel:'取消'};
}



function popupAddWindow(){
	var addButton = {
		text:'新增权限',
		iconCls:'icon-add',
		handler:addPrivilege
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeAddWindow
	};
	
	
	$("#privilegeDetailDiv").dialog({
		closed:false,
		modal:true,
		buttons:[addButton,cancelButton]
	});
	
}

function closeAddWindow(){
	$("#privilegeDetailDiv").dialog('close');
}

function addPrivilege(){
	if (!$('#privilegeForm').form('validate')) return;
	var formData=$('#privilegeForm').serialize();
	$.post(webappName+"/admin/privilegeManager/addPrivilege",formData,onAddPrivilegeComplete,"json");
	closeAddWindow();
}

function onAddPrivilegeComplete(data){
	$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	$("#privilegeTable").datagrid('reload');
}

function confirmRemove(){
	var rows = $('#privilegeTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要删除的项目');
		return;
	}
	$.messager.confirm('确认','确认要删除吗?',removePrivilege);
}

function removePrivilege(isConfirm){
	
	if (!isConfirm) return;
	
	var rows = $('#privilegeTable').datagrid('getSelections');
	

	var privilege;
	var codeList='';
	for(var i=0;i<rows.length;i++){
		privilege = rows[i];
		codeList += privilege.code + ',';
	}
	codeList = codeList.substring(0,codeList.length-1);
	
	var requestPara = 'privilegeList='+codeList;
	
	$.post(webappName+"/admin/privilegeManager/removePrivileges",requestPara,onRemovePrivilegeComplete,"json");
	
}

function onRemovePrivilegeComplete(data){
	
	$("#privilegeTable").datagrid('reload');
	
	var result = data.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
	

}

function editPrivilege(rowIndex){
	if (lastEditRowIndex!=rowIndex){
		$("#privilegeTable").datagrid('endEdit', lastEditRowIndex);
		$("#privilegeTable").datagrid('beginEdit', rowIndex);
	}
	lastEditRowIndex = rowIndex;
}

function commitPrivilegeUpdate(rowIndex, rowData, changes){
	
	var changedKeys = [];
	for(var key in changes){
		changedKeys.push(key);
	}
	if(changedKeys.length==0) return;
	
	$.post(webappName+"/admin/privilegeManager/updatePrivilege",$.param(rowData),onUpdatePrivilegeComplete,"json");
}

function onUpdatePrivilegeComplete(responseVO){
	console.debug(responseVO);
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"更新成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
}

function acceptChanges(){
	$("#privilegeTable").datagrid('acceptChanges');
	lastEditRowIndex = -1;
}
function rejectChanges(){
	$("#privilegeTable").datagrid('rejectChanges');
	lastEditRowIndex = -1;
}

function sendQueryResult(){
	
	var queryParams = $('#privilegeTable').datagrid('options').queryParams;
	queryParams.queryMode = queryMode;
	queryParams.simpleQueryString = $.trim($('#simpleQueryConditionTxt').val());
	queryParams.code = $.trim($('#privilegeCodeTxt').val());
	queryParams.description = $.trim($('#privilegeDescriptionTxt').val());
	$("#privilegeTable").datagrid('load');  
}